<?php


namespace Mob\Controller;

use Think\Controller;


class WxUserController extends BaseController
{
	 private $WxUser ;

    public function _initialize()
    {

    }

    public function Login()
    {
    if(!is_weixin())
    {
   $this->redirect('member/index',"请使用微信浏览器访问",3);
    }

    $wxUser = D('WxUser');

   $openid = $wxUser->getWxopenid();

    if($openid)
    {
   $isReg= $wxUser->isregister($openid);

   if($isReg)
   {

    $res= $wxUser->wxUserLogin($openid);
   }else
   {
   	$wxUser->initWxUser($openid);
   }
    }else
    {
     $this->redirect('member/index',"请使用帐号登录",3);
    }
     $this->redirect('activity/activitymap');

    }
    public function logout()
    {
     if(is_login())
     {
      D('WxUser')->WxUserLogOut();
      $data['status'] = 1;

        } else {
            $data['status'] = 0;
            $data['info'] = "退出失败！";
        }
        $this->ajaxReturn($data);
    }

    public function userLogin()
   {
   	 $wxCommon = D('WxCommon');
    if (!isset($_GET['code'])) {

    $this->loginPortal();

    }else
    {
      $code = $_GET['code'];
     $access = $wxCommon->get_Token($code);
     $openid = $access['openid'];
     $access_token = $access['access_token'];
     $userInfo = $wxCommon->get_UserInfo($openid,$access_token);
    // print_r($userInfo);
     $wxuser=  D('WxUser');
     $wxuser->initUserInfo($userInfo);
    $wxuser->wxUserLogin($userInfo['openid']);
    $redirect = cookie('redirect'); //获取cookie信息
    if($redirect)
    {
    cookie('redirect',null);  //将cookie信息删除
    redirect($redirect);   //重定义页面
    }else
    {
    $this->redirect('activity/activitymap');
    }
    }

   }
   public function indexPortal()
   {
   	if(!is_login())//判断是否没有登录
   	{
     $wxUser=  D('WxUser');
    $openid = $wxUser->getWxopenid();
    if($openid)
    {
     $isReg= $wxUser->isregister($openid);
   if($isReg)
   {
    $res= $wxUser->wxUserLogin($openid);
   }else
   {
  $wxCommon = D('WxCommon');
  $redirect_uri = ('http://'.$_SERVER['HTTP_HOST'].U('Mob/WxUser/autoLogin'));
  $url = $wxCommon->get_AuthorizeUrl($redirect_uri);
  header("Location:".$url);
  exit();
   }
    }
   	}
   $this->display();
   }

  public function syncAvatarImg()
  {
  	$uid = get_uid();
  	$sync = D('Sync');
   $res = $sync->wxUserImage($uid);
   if($res)
   {
   	$data=array('status'=>1,'msg'=>'同步成功');
   }else
   {
    $data=array('status'=>0,'msg'=>'同步失败');
   }
   $this->ajaxReturn($data);


  }
   public function loginPortal()
   {
   	$red_url = I('get.redirect',null,false);
    $wxUser=  D('WxUser');
    $openid = $wxUser->getWxopenid();
    $red_url = urldecode($red_url);   //urldecode解析重定义地址
    if($openid)
    {
   $isReg= $wxUser->isregister($openid);
   if($isReg)
   {

    $res= $wxUser->wxUserLogin($openid);
    if($red_url)
    {
    redirect($red_url);  //如果有地址的话，跳转回原来的地址
    }else  //没有地址的话，则直接跳到首页
    {
    $this->redirect('activity/activitymap');
    }
   }else
   {
  $wxCommon = D('WxCommon');
  if($red_url)
  {
  cookie('redirect',$red_url);  //设置cookie重定义页面
  }
  $redirect_uri = ('http://'.$_SERVER['HTTP_HOST'].U('Mob/WxUser/userLogin'));
  $url = $wxCommon->get_AuthorizeUrl($redirect_uri);
  header("Location:".$url);
  exit();
   }
    }
   }
   public function autoLogin()
   {
   	 $wxCommon = D('WxCommon');
    if (!isset($_GET['code'])) {

    $this->loginPortal();

    }else
    {
      $code = $_GET['code'];

     $access = $wxCommon->get_Token($code);
     $openid = $access['openid'];
     $access_token = $access['access_token'];
     $userInfo = $wxCommon->get_UserInfo($openid,$access_token);
    // print_r($userInfo);
     $wxuser=  D('WxUser');
     $wxuser->initUserInfo($userInfo);
    $wxuser->wxUserLogin($userInfo['openid']);
     $this->redirect('WxUser/indexPortal');

    }
   }









}